/*
 * JniApplication.h
 *
 *  Created on: 2018年8月28日
 *      Author: zhengboyuan
 */

#ifndef JNI_JNIAPPLICATION_H_
#define JNI_JNIAPPLICATION_H_

#include "MediaStorage.h"
#include "TDiscreteArray.h"
#include "SharedPtr.h"

#include <jni.h>
#include "TCriticalSection.h"


typedef std::shared_ptr< av::MediaStorageChannel >		MediaStorageChannelPtr;
typedef comn::DiscreteArray< MediaStorageChannelPtr, 128 >	MediaStorageChannelArray;


class JniApplication : public av::MediaStorageSink
{
public:
	static JniApplication& instance();

public:
	JniApplication();
	virtual ~JniApplication();

	static void init();
	static void quit();

	bool open(const char* dbpath);

	void close();

	bool isOpen();

	av::MediaStorage& getStorage();

	int openChannel(const char* device);
	void closeChannel(int handle);

	MediaStorageChannelPtr getChannel(int handle);

	void setJvm(JavaVM* jvm);

	void setListener(JNIEnv * env, jobject jlistener);

	void releaseListener(JNIEnv * env);

protected:
	virtual void onStorageEvent(const av::StorageEvent& event);

protected:
	void fireStorageEvent(const av::StorageEvent& event);

protected:
	comn::CriticalSection	m_cs;

	MediaStorageChannelArray	m_channels;
	JavaVM*	m_jvm;
	jobject	m_listener;

};

#endif /* JNI_JNIAPPLICATION_H_ */
